//
//  CustomerInfoCell.m
//  mendianjieke
//
//  Created by 响当当 on 7/31/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "CustomerInfoCell.h"

@interface CustomerInfoCell()
@property (nonatomic, strong)UIView *pointView;
@property (nonatomic, strong)UIView *lineView;
@property (nonatomic, strong)UILabel *contextLabel;
@property (nonatomic, strong)UIButton *checkOrderButton;
@property (nonatomic, strong)UIImageView *inImageView;
@property (nonatomic, strong)UIImageView *outImageView;
@property (nonatomic, strong)UIButton *checkButton;
@end

@implementation CustomerInfoCell

- (UIView *)pointView
{
    if (!_pointView)
    {
        _pointView = [[UIView alloc]initWithFrame:CGRectMake(25*kUIScaleBasePlus,3*kUIScaleBasePlus,8*kUIScaleBasePlus,8*kUIScaleBasePlus)];
        _pointView.layer.masksToBounds = YES;
        _pointView.layer.cornerRadius = _pointView.frame.size.height/2;
        _pointView.backgroundColor = LCHexColor(0xFF7A17);
    }
    return _pointView;
}

- (UIView *)lineView
{
    if (!_lineView)
    {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(29*kUIScaleBasePlus,CGRectGetMaxY(self.pointView.frame),1*kUIScaleBasePlus,12*kUIScaleBasePlus)];
        _lineView.backgroundColor = JSALineColor;
    }
    return _lineView;
}

- (UILabel *)contextLabel
{
    if (!_contextLabel)
    {
        _contextLabel = [Utilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.pointView.frame)+6*kUIScaleBasePlus,0,300*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                         textAligment:NSTextAlignmentLeft
                                             textClor:LCHexColor(0x282828)
                                             textFont:13*kUIScaleBasePlus];
        _contextLabel.numberOfLines = 0;
    }
    return _contextLabel;
}

- (UIButton *)checkOrderButton
{
    if (!_checkOrderButton)
    {
        
    }
    return _checkOrderButton;
}

- (UIImageView *)inImageView
{
    if (!_inImageView)
    {
        _inImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
    }
    return _inImageView;
}

- (UIImageView *)outImageView
{
    if (!_outImageView)
    {
        _outImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
    }
    return _outImageView;
}

- (UIButton *)checkButton
{
    if (!_checkButton)
    {
      _checkButton = [Utilties creatButtonWithFrame:CGRectZero
                                       textAligment:UIControlContentHorizontalAlignmentCenter
                                               text:@"查看清单"
                                           textClor:LCHexColor(0xFF7A17)
                                          buttonBGC:[UIColor clearColor]
                                           textFont:13*kUIScaleBasePlus
                                             target:self
                                             method:@selector(checkOrder)];
    }
    return _checkButton;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self.contentView addSubview:self.pointView];
        [self.contentView addSubview:self.lineView];
        [self.contentView addSubview:self.contextLabel];
        [self.contentView addSubview:self.inImageView];
        [self.contentView addSubview:self.outImageView];
        [self.contentView addSubview:self.checkButton];

    }
    return self;
}

#pragma mark - set model

- (void)setModel:(TableViewModel *)model
{
    _model = model;
    
    NSArray *arr = [model.text componentsSeparatedByString:@","];
    
    if (arr.count == 1)
    {
        self.contextLabel.text = model.text;
        [self.contextLabel sizeToFit];
    }
    else
    {
        UILabel *label = [Utilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.pointView.frame)+6*kUIScaleBasePlus,0,300*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                          textAligment:NSTextAlignmentLeft
                                              textClor:LCHexColor(0x282828)
                                              textFont:13*kUIScaleBasePlus];
        label.text = arr[1];
        [self.contentView addSubview:label];
        [label sizeToFit];
        
        UIImageView *inShopView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),0,15*kUIScaleBasePlus,15*kUIScaleBasePlus)];
        NSString *expressionStr = arr[0];
        switch (expressionStr.integerValue)
        {
            case 0:
            {
                inShopView.image = [UIImage imageNamed:@"beishang"];
            }
                break;
            case 1:
            {
                inShopView.image = [UIImage imageNamed:@"beishang"];
            }
                break;
            case 2:
            {
                inShopView.image = [UIImage imageNamed:@"shengqi"];
            }
                break;
            default:
                break;
        }
        [self.contentView addSubview:inShopView];
        
        [self.contextLabel setX:CGRectGetMaxX(inShopView.frame)+3*kUIScaleBasePlus];
        self.contextLabel.text = arr[2];
        [self.contextLabel sizeToFit];
        
        UIImageView *outShopView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.contextLabel.frame)+5*kUIScaleBasePlus,0,15*kUIScaleBasePlus,15*kUIScaleBasePlus)];
        NSString *outExpresionStr = arr[3];
        switch (outExpresionStr.integerValue)
        {
            case 0:
            {
                outShopView.image = [UIImage imageNamed:@"beishang"];
            }
                break;
            case 1:
            {
                outShopView.image = [UIImage imageNamed:@"pingjing"];
            }
                break;
            case 2:
            {
                outShopView.image = [UIImage imageNamed:@"kaixin"];
            }
                break;
            default:
                break;
        }
        [self.contentView addSubview:outShopView];
        
        NSString *haveOrder = [arr lastObject];
        if (haveOrder.integerValue > 0)
        {
            [self.checkButton setFrame:CGRectMake(CGRectGetMaxX(label.frame)+30*kUIScaleBasePlus,CGRectGetMaxY(label.frame)+5*kUIScaleBasePlus,80*kUIScaleBasePlus,20*kUIScaleBasePlus)];
        }
        else
        {
            [self.checkButton setFrame:CGRectZero];
        }
        
    }
    
    if (model.num.integerValue == -1)
    {
        self.lineView.hidden = NO;
        [self.lineView setHeight:model.cellHeight-10.5*kUIScaleBasePlus];
    }
    else
    {
        self.lineView.hidden = YES;
    }
    
}

- (void)checkOrder
{
    
}

#pragma mark - 计算cell高度
/** 获取cell高度 */
+ (CGFloat)heightWithModel:(TableViewModel *)model
{
    NSString *string = model.text;
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:string];
    
    NSRange allRange = [string rangeOfString:string];
    
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:allRange];
    
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor]range:allRange];
    
    CGFloat titleHeight;
    
    NSStringDrawingOptions options =  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    
    // 获取label的最大宽度
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, CGFLOAT_MAX)options:options context:nil];
    
    titleHeight = ceilf(rect.size.height);
    
    return titleHeight + 30*kUIScaleBasePlus; // 动态高度 + 静态高度
}

@end
